import asyncio

from loguru import logger

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.parse.jzy_major_parse import MajorParser

from com.arcfox.school.parse.yifen_yiduan_parse import YifenYiduanParser
from com.arcfox.school.processor.jzy.jzy_yifen_yiduan_processor import YifenYiduanProcessor
from com.arcfox.school.spider.jzy.jzy_base_spider import JZYBaseSpider
from com.arcfox.util import async_request as requests
from com.arcfox.util.util import random_sleep

'''
    一分一段
'''


class YifenYiduanDetailSpider(JZYBaseSpider):
    def __init__(self):
        super().__init__()
        self.start_url = "http://www.juzhiyuan.com/yifenyiduan?rand=85&province=34"
        self.processor = YifenYiduanProcessor()
        self.parser = YifenYiduanParser()
        self.task_manager = RedisTaskManager(self.TASK_SEGMENT_LIST_JZY)

    async def _pull_task(self):
        return await self.task_manager.pull_tasks(10), ExecuteType.FINISH

    async def _crawl_by_task(self, tasks):
        await self.init_session()
        for task in tasks:
            logger.info(f"开始爬取{task}")
            try:
                resp = await requests.get(self.session, url=task["url"], headers=self.get_default_header())
                if resp.code == 200:
                    result_list = self.parser.parse_yifen_yiduan_result(task, resp.response)
                    if len(result_list) == 0:
                        logger.info("未获取到一分一段信息")
                        continue
                    await self.processor.store_yifen_yiduan(tasks=result_list)
            except Exception as e:
                logger.error("命中风控, 休眠10分钟:{}",e)
                await self.task_manager.add_fail_tasks(task)

                await random_sleep(10 * 60)
        else:
            await self.task_manager.add_fail_tasks(task)


if __name__ == "__main__":
    asyncio.run(YifenYiduanDetailSpider().start_crawl())
